<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use app\admin\model\Pic;
use think\db\Where;
use think\Model;

class ShopGoods extends Model {
    protected $autoWriteTimestamp = false;

    /**
     * @param string $q
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($q='',$where=[],$order='create_time desc',$limit=10,$filed='*',$out=false){
        if($q){
            $list = $this
                ->field($filed,$out)
                ->with(['shop'=>function($query){
                    $query->field('id,title,shop_type,contact,real_name,location,is_vip,is_vip');
                }])
                ->where($where)
                ->whereRaw("match(title) against('+{$q}' in boolean mode)")
                ->order($order)
                ->paginate($limit);
        }else{
            $list = $this
                ->field($filed,$out)
                ->with(['shop'=>function($query){
                    $query->field('id,title,shop_type,contact,real_name,location,is_vip,is_vip');
                }])
                ->where($where)
                ->order($order)
                ->paginate($limit);
        }

//        foreach ($list as $k => &$v){
//            $v['message'] = $message->where('aid','eq',$v['id'])->count();
//            $v['collection'] = $collection->where('aid','eq',$v['id'])->count();
//        }
        return $list;
    }

    /**
     * @param array $where
     * @param string $order
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllList($where=[],$order='create_time desc'){
        $list = $this
            ->field('create_time',true)
            ->with(['shop'=>function($query){
                $query->field('id,title,shop_type,contact,real_name,location,is_vip,is_vip');
            }])
            ->where($where)
            ->order($order)
            ->select();
        return $list;
    }

    /**
     * @param int $uid
     * @param int $shop_id
     * @param string $title
     * @param string $information
     * @param string $image
     * @param string $pics
     * @param string $content
     * @param string $price
     * @param string $discount
     * @param int $stocks
     * @param string $out
     * @return bool
     */
    public function addNew($uid,$shop_id=0,$title='',$information='',$image='',$pics='',$content='',$price='',$discount='',$stocks=0,&$out=''){
        $data = [
            'shop_id'=>$shop_id,
            'uid'=>$uid,
            'title'=>$title,
            'information'=>$information,
            'image'=>$image,
            'pics'=>$pics,
            'content'=>$content,
            'price'=>$price,
            'discount'=>$discount,
            'stocks'=>$stocks,
            'status'=>1,
            'create_time'=>time(),
        ];
        $valid = new \app\first\validate\ShopGoods();
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
//        $count = $this->where('title','eq',$title)->count('1');
//        if($count){
//            $out = '存在一个类似的梦想';
//            return false;
//        }
        if(!$this->save($data)){
            $out = '添加失败';
            return false;
        }
        return true;
    }


    /**
     * @param int $id
     * @param string $filed
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneById($id=0,$filed=''){
        $filed = $filed?$filed:'update_time';
        $info = $this->field($filed,true)->find($id);
        $info->hits = $info['hits'] + 1;
        $info->save();
        $res = $this
            ->with(['shop'=>function($query){
                $query->field('id,title,shop_type,contact,real_name,location,is_vip,is_vip');
            }])
            ->where('id','eq',$id)
            ->find();
        if($res){
            $res['create_user'] = (new Member())->field('id,account,avatar,nickname')->find($info['uid']);
        }
        return $res;
    }

    public function shop(){
        return $this->hasOne('shop','id','shop_id');
    }

    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getImageAttr($value){
        if(empty($value)){
            return '';
        }
        $pic = new Pic();
        return $pic->getUrlById($value);
    }

    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPicsAttr($value){
        if(empty($value)){
            return '';
        }
        $pic = new Pic();
        return $pic->getUrlsById(explode(',',$value));
    }

    /**
     * @param $value
     * @return string
     */
    public function setContentAttr($value){
        if($value){
            return htmlspecialchars($value);
        }else{
            return '';
        }
    }

    /**
     * @param $value
     * @return string
     */
    public function getContentAttr($value){
        if($value){
            return htmlspecialchars_decode($value);
        }else{
            return '';
        }
    }
}